package org.db2.project.airline.dao.impl;

import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.dao.FlightClassDAO;
import org.db2.project.airline.model.FlightClass;
import org.db2.project.airline.model.FlightClassType;
import org.db2.project.airline.model.FlightPlan;
import org.hibernate.HibernateException;

/**
 *
 */
public class HibernateFlightClassDAO extends HibernateGenericDAO<FlightClass> implements FlightClassDAO {

    public HibernateFlightClassDAO() {
        // empty constructor
    }

    @Override
    public FlightClass find(FlightPlan flightPlan, FlightClassType flightClassType) throws DAOException {

        try {
            return (FlightClass) getSession().createQuery("select flightClass " +
                    " from FlightClass flightClass, " +
                    " Airplane airplane," +
                    " Flight flight, " +
                    " FlightPlan flightPlan " +
                    " where " +
                    " flightClass.airplane.id = airplane.id and " +
                    " flight.airplane.id = airplane.id and " +
                    " flightPlan.flight.id = flight.id and" +
                    " flightPlan.id = :flightPlanId and " +
                    " flightClass.flightClassType.id = :flightClassTypeId")
                    .setParameter("flightPlanId", flightPlan.getId())
                    .setParameter("flightClassTypeId", flightClassType.getId())
                    .uniqueResult();
        } catch (HibernateException exc) {
            throw new DAOException("Error occurred when retrieving entity from persistence.", exc);
        }
    }
}
